package fr.upstlse.glre.client.espacededie.commun.depot;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreFilter;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.espacededie.commun.depot.action.FilterSelectionAction;
import fr.upstlse.glre.client.espacededie.commun.depot.action.AbstractViewerSelectionAction;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.depot.DocumentVOGWT;
import fr.upstlse.glre.client.vo.depot.LibelleVOGWT;

public class Telecharger extends Composite {
	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	private ContentPanel telechargerContentPanel;

	private DataList telechargerPanel = new DataList();

	private Collection<DocumentVOGWT> documentVOGWTs;;
	private Collection<LibelleVOGWT> libelleVOGWTs;

	private String libelleCourant;
	private AbstractViewerSelectionAction viewerSelectionAction;
	ListStore<ModelData> store;

	private static Telecharger instance;

	public Telecharger() {
		instance = this;

		createTelechargerContentPanel();
		libelleCourant = "";

		initWidget(telechargerContentPanel);
	}

	private void createTelechargerContentPanel() {
		telechargerContentPanel = new ContentPanel();
		telechargerContentPanel.setAutoWidth(true);
		telechargerContentPanel.setWidth(500);
		telechargerContentPanel.setId(RfgConstante.TELECHARGER_PANEL_ID);
		telechargerContentPanel.setBorders(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(telechargerContentPanel,
				telechargerContentPanel.getHeader());

		final Button lAfficherDocuments = new Button();
		lAfficherDocuments.setText(RfgConstante.TELECHARGER_BUTTON_AFFICHER);
		lAfficherDocuments
				.addSelectionListener(new SelectionListener<ComponentEvent>() {

					public void componentSelected(ComponentEvent ce) {
						createTelechargerPanel();
						telechargerContentPanel.add(telechargerPanel);
						telechargerContentPanel.layout();
						lAfficherDocuments.setVisible(false);
					}
				});
		telechargerContentPanel
				.setButtonAlign(Style.HorizontalAlignment.CENTER);
		telechargerContentPanel.addButton(lAfficherDocuments);

		telechargerContentPanel
				.setHeading(RfgConstante.TELECHARGER_PANEL_HEADER_TEXT);
	}

	public void createTelechargerPanel() {
		telechargerPanel.setStyleName("telechargerPanel");
		telechargerPanel.setFlatStyle(true);

		final MessageBox lAttente = MessageBox.wait("Chargement",
				"Chargement des documents...", "Veuillez patienter");
		// Timer timer = new Timer() {
		// @Override
		// public void run() {
		constructDocumentsPanel(lAttente);
		// }
		// };
		// timer.schedule(500);

	}

	private List<ModelData> getDocuments() {
		ArrayList<ModelData> lListDocumentVOGWT = new ArrayList<ModelData>();

		for (DocumentVOGWT lDocumentVOGWT : documentVOGWTs) {
			BaseModelData lDocumenData = new BaseModelData();
			lDocumenData.set(RfgConstante.DEPOT_MODELDATA_ID, lDocumentVOGWT
					.getId());
			lDocumenData.set(RfgConstante.DEPOT_MODELDATA_INTITULE,
					lDocumentVOGWT.getIntitule());
			lDocumenData.set(RfgConstante.DEPOT_MODELDATA_DESCRIPTION,
					lDocumentVOGWT.getDescription());
			lDocumenData.set(RfgConstante.DEPOT_MODELDATA_LIBELLE,
					lDocumentVOGWT.getLibelle().getIntitule());
			lDocumenData.set(RfgConstante.DEPOT_MODELDATA_TAILLE,
					lDocumentVOGWT.getTaille());

			lListDocumentVOGWT.add(lDocumenData);
		}
		return lListDocumentVOGWT;
	}

	private List<ModelData> getLibelles() {
		ArrayList<ModelData> lLibelleVOGWTs = new ArrayList<ModelData>();

		for (LibelleVOGWT lLibelleVOGWT : libelleVOGWTs) {
			BaseModelData lLibelleData = new BaseModelData();
			lLibelleData.set(RfgConstante.LIBELLE_MODELDATA_ID, lLibelleVOGWT
					.getId());
			lLibelleData.set(RfgConstante.LIBELLE_MODELDATA_INTITULE,
					lLibelleVOGWT.getIntitule());
			lLibelleVOGWTs.add(lLibelleData);
		}

		return lLibelleVOGWTs;
	}

	private ComboBox createFilterComboBox() {
		ListStore<ModelData> lStore = new ListStore<ModelData>();
		lStore.add(getLibelles());

		ComboBox lComboBox = new ComboBox();
		lComboBox.setDisplayField(RfgConstante.LIBELLE_MODELDATA_INTITULE);
		lComboBox.setWidth(210);
		lComboBox.setStore(lStore);

		return lComboBox;
	}

	private void constructDocumentsPanel(final MessageBox lAttente) {
		AsyncCallback<Collection<LibelleVOGWT>> lCallbackLibelle = new AsyncCallback<Collection<LibelleVOGWT>>() {

			public void onFailure(Throwable pCaught) {
				MessageBox.alert("Erreur getAllLibelle ", pCaught.getMessage(),
						null);
			}

			public void onSuccess(Collection<LibelleVOGWT> pResult) {
				libelleVOGWTs = pResult;
				constructDocuments(lAttente);
			}
		};
		ControlerUtil.getControlerServiceAsyncInstance().getAllLibelles(
				lCallbackLibelle);
	}

	private void constructDocuments(final MessageBox lAttente) {
		AsyncCallback<Collection<DocumentVOGWT>> lCallback = new AsyncCallback<Collection<DocumentVOGWT>>() {

			public void onFailure(Throwable pCaught) {
				lAttente.hide();
				MessageBox.alert(
						"Erreur getAllDocuments ", pCaught.getMessage(), null);
			}

			public void onSuccess(Collection<DocumentVOGWT> pResult) {
				lAttente.hide();
				documentVOGWTs = pResult;

				store = new ListStore<ModelData>();
				store.add(getDocuments());

				DataListBinder<ModelData> binder = new DataListBinder<ModelData>(
						telechargerPanel, store);
				binder
						.setDisplayProperty(RfgConstante.DEPOT_MODELDATA_INTITULE);
				binder.setStringProvider(new ModelStringProvider<ModelData>() {

					public String getStringValue(ModelData model,
							String property) {
						String lTexte = (String) model
								.get(RfgConstante.DEPOT_MODELDATA_INTITULE);
						lTexte += " - "
								+ (String) model
										.get(RfgConstante.DEPOT_MODELDATA_DESCRIPTION);
						return lTexte;
					}

				});
				binder.init();

				final StoreFilter<ModelData> filter = new StoreFilter<ModelData>() {

					public boolean select(Store store, ModelData parent,
							ModelData item, String property) {
						if (((String) item
								.get(RfgConstante.DEPOT_MODELDATA_LIBELLE))
								.compareTo(libelleCourant) != 0) {
							return false;
						} else {
							return true;
						}
					}
				};
				store.addFilter(filter);

				telechargerPanel.addListener(Events.SelectionChange,
						viewerSelectionAction);

				final ComboBox lFilterComboBox = createFilterComboBox();
				lFilterComboBox.addListener(Events.Select,
						new FilterSelectionAction(Telecharger.getInstance(),
								store, filter, lFilterComboBox));

				telechargerContentPanel.getHeader().addTool(lFilterComboBox);
				telechargerContentPanel.add(lFilterComboBox);
				Info.display("Information",
						"Succès du chargement des documents", "");
			}

		};
		ControlerUtil.getControlerServiceAsyncInstance().getAllDocuments(
				lCallback);
	}

	public ContentPanel getTelechargerContentPanel() {
		return telechargerContentPanel;
	}

	public void setTelechargerContentPanel(ContentPanel telechargerContentPanel) {
		this.telechargerContentPanel = telechargerContentPanel;
	}

	public DataList getTelechargerPanel() {
		return telechargerPanel;
	}

	public void setTelechargerPanel(DataList telechargerPanel) {
		this.telechargerPanel = telechargerPanel;
	}

	public Collection<DocumentVOGWT> getDocumentVOGWTs() {
		return documentVOGWTs;
	}

	public void setDocumentVOGWTs(Collection<DocumentVOGWT> documentVOGWTs) {
		this.documentVOGWTs = documentVOGWTs;
	}

	public Collection<LibelleVOGWT> getLibelleVOGWTs() {
		return libelleVOGWTs;
	}

	public void setLibelleVOGWTs(Collection<LibelleVOGWT> libelleVOGWTs) {
		this.libelleVOGWTs = libelleVOGWTs;
	}

	public String getLibelleCourant() {
		return libelleCourant;
	}

	public void setLibelleCourant(String libelleCourant) {
		this.libelleCourant = libelleCourant;
	}

	public static Telecharger getInstance() {
		return instance;
	}

	public static void setInstance(Telecharger instance) {
		Telecharger.instance = instance;
	}

	public AbstractViewerSelectionAction getViewerSelectionAction() {
		return viewerSelectionAction;
	}

	public void setViewerSelectionAction(
			AbstractViewerSelectionAction viewerSelectionAction) {
		this.viewerSelectionAction = viewerSelectionAction;
	}

	public ListStore<ModelData> getStore() {
		return store;
	}

	public void setStore(ListStore<ModelData> store) {
		this.store = store;
	}
}